/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet.ogrenci;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import siniflar.Ders;
import siniflar.Ogrenci;
import vt.Veritabani;

/**
 *
 * @author kalemsel
 */
public class DersKaydiYap extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        
        HttpSession oturum = request.getSession();
        
        Boolean ogrenciDurum = (Boolean) oturum.getAttribute("ogrenciDurum");
        if (ogrenciDurum == null) {
            ogrenciDurum = false;
        }

        if(ogrenciDurum){
            Ogrenci ogrenci = (Ogrenci)oturum.getAttribute("ogrenci");

            Veritabani vt = new Veritabani();
            String[] dersidleri = request.getParameterValues("dersler");

            if(dersidleri == null) {
                oturum.setAttribute("ogrenciDersKayitDurum", "Ders kaydı için ders seçiniz.");
                //yonlendir(request, response, "DersKayit");
                response.sendRedirect("DersKayit");
                return;
            }

            ArrayList<Integer> dersler = dersOlustur(dersidleri);

            for(int ders : dersler) {
                vt.ogrenciDersAta(ogrenci, vt.dersVer(ders));
            }

            oturum.setAttribute("ogrenciDersKayitDurum", "Ders kaydı yapıldı.");
            yonlendir(request, response, "derskayitdurum.jsp");
            return;
        }
        else {
            oturum.setAttribute("hata", "Yetkisiz giriş denemesi");
            oturum.setAttribute("ogrenciDurum", false);
            yonlendir(request, response, "giris.jsp");
            return;
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect("DersKayit");
        return;
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private ArrayList<Integer> dersOlustur(String[] dersidleri) {
        ArrayList<Integer> dersler = new ArrayList<Integer>();
        for(String d : dersidleri) {
            dersler.add(Integer.valueOf(d));
        }
        return dersler;
    }
    
    public void yonlendir(HttpServletRequest request, HttpServletResponse response, String adres)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher(adres);
        dispatcher.forward(request, response);
    }
}
